class Solution:
    def minSubarray(self, nums: list, p: int) -> int:
        remainder = sum(nums)
        res = len(nums)
        preSum = 0
        memo = {0: -1}
        for i in range(len(nums)):
            preSum += nums[i]
            key = (preSum % p - remainder % p + p) % p
            memo[preSum % p] = i
            if key in memo:
                res = min(res, i - memo[key])

        return res if res < len(nums) else -1
